/**
 * <b>项目名称：</b>网络应用框架<br/>
 * <b>包    名：</b>com.hhwy.plugin<br/>
 * <b>文 件 名：</b>ApplicationContext.java<br/>
 * <b>版本信息：</b><br/>
 * <b>日    期：</b>2015年3月5日-下午4:15:54<br/>
 * <b>Copyright (c)</b> 2015恒华伟业科技股份有限公司-版权所有<br/>
 * 
 */
package com.hhwy.framework.persistent;

import java.io.IOException;
import java.util.HashMap;

import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.core.io.Resource;
import org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver;
import org.springframework.instrument.classloading.LoadTimeWeaver;
import org.springframework.orm.jpa.JpaDialect;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaDialect;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import com.hhwy.framework.datasource.DatasourceHelper;
import com.hhwy.framework.datasource.DynamicDataSource;
import com.hhwy.framework.persistent.dialect.HibernateJpaVendorExtAdapter;
import com.hhwy.framework.util.IoTools;

 /**
 * <b>类 名 称：</b>ApplicationContext<br/>
 * <b>类 描 述：</b>持久层对象配置，加载mybatis配置文件和扫描JPA实体<br/>
 * <b>创 建 人：</b>lihuiming<br/>
 * <b>修 改 人：</b>lhm<br/>
 * <b>修改时间：</b>2015年3月5日 下午4:15:54<br/>
 * <b>修改备注：</b><br/>
 * @version 1.0.0<br/>
 */
@Configuration
@EnableTransactionManagement
@EnableAspectJAutoProxy
public class DaoConfig {
	
	private Resource getSqlMapConfigResource(){
		/** mybatis配置文件 */
		Resource sqlMapConfigResource = null;
		try {
			sqlMapConfigResource = IoTools.scan("/config/maps/"+ DatasourceHelper.DATABASE_TYPE,
					"/sqlmap-config.xml")[0];
		} catch (IOException e) {
			e.printStackTrace();
		}
		return sqlMapConfigResource;
	}
	
	private Resource[] getMapperResources(){
		/** mybatis映射文件 */
		Resource[] mapperResources = null;

		try {
			mapperResources = IoTools.scan("/config/maps/"
					+ DatasourceHelper.DATABASE_TYPE, "/**/*-mapper.xml");
		} catch (IOException e) {
			e.printStackTrace();
		}
		return mapperResources;
	}
	
	@Autowired
	private DynamicDataSource dataSource;
	@Autowired(required=false)
	private HashMap<String,String> entityMap;
	
	@Bean
	public LoadTimeWeaver loadTimeWeaver() {
		return new InstrumentationLoadTimeWeaver();
	}

	@Bean
	public JpaDialect jpaDialect() {
		return new HibernateJpaDialect();
	}

	@Bean
	public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
		LocalContainerEntityManagerFactoryBean lef = new LocalContainerEntityManagerFactoryBean();
		lef.setDataSource(dataSource);
		lef.setJpaVendorAdapter(jpaVendorAdapter());
		lef.setJpaDialect(jpaDialect());
		lef.setLoadTimeWeaver(loadTimeWeaver());
		if(entityMap == null){
			//插件单独运行时，会扫描两次，appContext和springmvc，框架appContext扫描时，map为空，默认包名为com.hhwy
			lef.setPackagesToScan("com.hhwy");
		} else {
			lef.setPackagesToScan(entityMap.get("basePackage").split(","));
		}
		return lef;
	}

	@Bean
	public PlatformTransactionManager transactionManager() {
		JpaTransactionManager txManager = new JpaTransactionManager();
		txManager.setEntityManagerFactory(entityManagerFactory()
				.getNativeEntityManagerFactory());
		
		return txManager;
	}

	@Bean
	public JpaVendorAdapter jpaVendorAdapter() {
		HibernateJpaVendorExtAdapter hibernateJpaVendorAdapter = new HibernateJpaVendorExtAdapter();
		hibernateJpaVendorAdapter.setShowSql(DatasourceHelper.isShowSql());
		hibernateJpaVendorAdapter.setGenerateDdl(DatasourceHelper.isGeneralDDL());
		hibernateJpaVendorAdapter.setDatabase(DatasourceHelper.getDatabase());
		
		return hibernateJpaVendorAdapter;
	}
	
	@Bean
	public SqlSessionTemplate getSqlSessionTemplate() throws Exception {
		SqlSessionTemplate sqlSessionTemplate = new SqlSessionTemplate(
				getSqlSessionFactoryBean().getObject());

		return sqlSessionTemplate;
	}

	@Bean
	public SqlSessionFactoryBean getSqlSessionFactoryBean() {

		SqlSessionFactoryBean ssfb = new SqlSessionFactoryBean();
		ssfb.setDataSource(dataSource);
		ssfb.setConfigLocation(getSqlMapConfigResource());
		ssfb.setMapperLocations(getMapperResources());

		return ssfb;
	}
}
